package com.jc.service.h5;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.HttpUtil;
import com.jc.ds.model.SystemLogList;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.service.model.h5.H5RespModel;
import com.jc.service.systemloglist.SystemLogListService;

@Service
public class PushServiceImpl implements PushService {
	private final Logger LOGGER = LoggerFactory.getLogger(PushServiceImpl.class);
	
	@Autowired
	private SystemLogListService  systemLogListService;
	
	
	public H5RespModel pushH5(String url,String body) {
		H5RespModel retMode = new H5RespModel();
		SystemLogList loglist = new SystemLogList();
		boolean isOK = false;
		String ret = null;
		try {
			ret = HttpUtil.postJson(url, body, null);
			
			
			JSONObject retObject = JSONObject.parseObject(ret);
			if(retObject != null) {
				retMode.setReturnCode(retObject.getString("returnCode"));
				retMode.setReturnMsg(retObject.getString("returnMsg"));
				if(StringUtils.equals("0", retObject.getString("returnCode"))) {
					isOK = true;
				}
				//return retMode;
			}else {
				isOK = false;
				retMode.setReturnCode("1");
				retMode.setReturnMsg("向H5商城推送失败, 返回值为空!");
				//return retMode;
			}
			
			if(isOK) {
				loglist.setBusinessData(body);
				loglist.setConclusion(Conclusion.OK.getName());
				loglist.setLogType(LogType.PUSH_H5_PRODUCT.getName());
				loglist.setCreateTime(new Date());
				loglist.setLogTypeName(LogType.PUSH_H5_PRODUCT.getDesc());
				loglist.setRespText(ret);
				systemLogListService.add(loglist, false);
			}else {
				//记录系统日志
				loglist.setBusinessData(body);
				loglist.setConclusion(Conclusion.FAIL.getName());
				loglist.setLogType(LogType.PUSH_H5_PRODUCT.getName());
				loglist.setCreateTime(new Date());
				loglist.setExceptionMsg(retObject.getString("returnMsg"));
				loglist.setLogTypeName(LogType.PUSH_H5_PRODUCT.getDesc());
				loglist.setRespText(ret);
				systemLogListService.add(loglist, false);	
			}
			return retMode;
		}catch (Exception e) {
			LOGGER.error("推送产品过程中出现网络错误, {},{}" , e.getMessage(),e);
			retMode.setReturnCode("1");
			if(e != null && StringUtils.isNotBlank(e.getMessage())) {
				retMode.setReturnMsg("推送失败 : " + StringUtils.substring(e.getMessage(), 0,150));
			}
			
			loglist.setBusinessData(body);
			loglist.setConclusion(Conclusion.FAIL.getName());
			loglist.setLogType(LogType.PUSH_H5_PRODUCT.getName());
			loglist.setCreateTime(new Date());
			if(e != null && StringUtils.isNotBlank(e.getMessage())) {
				loglist.setExceptionMsg(StringUtils.substring(e.getMessage(), 0,2000));
			}
			
			loglist.setLogTypeName(LogType.PUSH_H5_PRODUCT.getDesc());
			loglist.setRespText(ret);
			systemLogListService.add(loglist, false);	
			
			
			
			return retMode;
		}
	}
}
